package WikiPT;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Relations {
	
	public static Map<String, String> aggregatedRelations = null; 		

	public static String[] ignore = {"affiliation","anthem","award","beatifiedPlace","canonizedPlace","capital","chairman","citizenship","city","club","commune","distributingCompany","distributingLabel","division","employer","ethnicity","garrison","governmentType","heir","instrument","largestCountry","leader","lessPopulatedCountry","lessPopulousCountry","locationCountry","maintainedBy","majorShrine","majorVolcano","mayor","militaryUnit","mostPopulatedCountry","mostPopulousCountry","notableWork","occupation","officialLanguage","oliticGovernmentDepartment","owner","owningCompany","owningOrganisation","parentCompany","patent","picture","poorestCountry","parent","politicGovernmentDepartment","portrayer","president","product","recordLabel","regionServed","richestCountry","saint","servingRailwayLine","smallestCountry","sportCountry","spouse","subsidiary","tenant","timeZone"};
	
	public static String other[] = {"number","relative","crosses","neighboringMunicipality","sisterStation","portrayer","patron","knownFor","ground",
									"tenant","appearancesInNationalTeam","riverMouth","sourceMountain","broadcastArea"};

	public static String[] changeDirection = {"predecessor","doctoralAdvisor","influenced","child","foundedBy",
											  "capitalCountry","currentMember","pastMember","keyPerson","president","leaderName",
											  "successor","capital","parent","largestCity"};
	
	public static void initialize() {
		aggregatedRelations = new HashMap<String, String>();
		
		//largestCity
		String locatedInArea[] = {"locatedInArea","archipelago","location", "locationCity",
								  "locationCountry","municipality","subregion","federalState","district","region",
								  "province","state","county", "map","campus","department","country"};		
		
		String partOf[] = {"type","parentOrganisation","distributingCompany","broadcastNetwork","affiliation",
						   "university","youthClub","party","pastMember","team","associatedMusicalArtist","member","associatedBand","currentMember"};						
		
		String origin[] = {"origin","birthPlace","foundationPlace","sourcePlace","headquarter","nationality","residence","hometown","sportCountry","citizenship"};						
		
		String deathOrBurialPlace[] = {"deathPlace","placeOfBurial"};		
		
		String partner[] = {"spouse","partner"};

		//trocar direcção para encaixar em "e1, keyperson in e2"
		//keyPerson
		//president
		//leaderName
		//foundedBy
		String keyPerson[] = {"keyPerson","president","monarch","leader","leaderName","founder"};		
		
		String influencedBy[] = {"influencedBy"};
		
		String parent[] = {"parent"};
		
		//trocar direcção para encaixar em "e1, succesor of e2"
		String successor[] = {"successor"};	
		
		addRelations(locatedInArea,"locatedInArea");
		addRelations(partOf,"partOf");
		addRelations(origin,"origin");		
		addRelations(deathOrBurialPlace,"deathOrBurialPlace");
		addRelations(partner,"partner");
		addRelations(keyPerson,"keyPerson");
		addRelations(influencedBy,"influencedBy");
		addRelations(successor,"successor");
		addRelations(parent,"parent");
		addRelations(other,"other");
		
		Set<String> relations = new HashSet<String>();		
		for (String relation : aggregatedRelations.keySet()) {			
			relations.add(aggregatedRelations.get(relation));
		}
	}
	
	public static void addRelations(String[] lowerRelations, String relationName) {
			for (String r : lowerRelations) {
				aggregatedRelations.put(r, relationName);
			}
	}

	public static String changeRelationType(String type_original,String e1, String e2){
		String type = null;
		if (!Arrays.asList(ignore).contains(type_original.trim())) {	
			if (!Arrays.asList(Relations.changeDirection).contains(type_original)) {
				//transform relationship type into a top aggregated type
				type  = Relations.aggregatedRelations.get(type_original);												
			}				
			else {
				//keyPerson, president, leaderName to keyPerson
				if (type_original.equals("keyPerson") || type_original.equals("foundedBy") || type_original.equals("president") || type_original.equals("leaderName") || type_original.equals("manager")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "keyPerson";
				}

				//currentMember and pastMember to partOf  
				if (type_original.equals("currentMember") || type_original.equals("pastMember")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "partOf";
				}
					
				//capitalCountry to locatedinArea
				if (type_original.equals("capitalCountry") || type_original.equals("city")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "locatedInArea";
				}
				
				//more examples of "influencedBy"
				if (type_original.equals("influenced") || type_original.equals("doctoralAdvisor")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "influencedBy";
				}

				//more examples of "successor"
				if (type_original.equals("predecessor") || type_original.equals("successor")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "successor";
				}
				
				//more examples of "parent"
				if (type_original.equals("child")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "parent";
				}						
				
				//more examples of "keyPerson"
				if (type_original.equals("foundedBy")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "keyPerson";
				}
				
				//largestCity to locatedInArea						  
				if (type_original.equals("largestCity")) {
					String tmp = e2;
					e2 = e1;
					e1 = tmp;
					type = "locatedInArea";							
				}							
			}
		return type+'|'+e1+'|'+e2;
		}
		return "ignore|ignore|ignore|";
	}
}
